

package com.hazelcast.internal.cluster.impl.operations;

import com.hazelcast.cluster.Address;
import com.hazelcast.internal.cluster.impl.ClusterDataSerializerHook;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;

import java.io.IOException;

/**
 * Operation sent by any node to set the master address on the receiver
 */
public class MasterResponseOp extends AbstractClusterOperation {

    protected Address masterAddress;

    public MasterResponseOp() {
    }

    public MasterResponseOp(Address originAddress) {
        this.masterAddress = originAddress;
    }

    @Override
    public void run() {
        ClusterServiceImpl clusterService = getService();
        clusterService.getClusterJoinManager().handleMasterResponse(masterAddress, getCallerAddress());
    }

    public Address getMasterAddress() {
        return masterAddress;
    }

    @Override
    protected void readInternal(final ObjectDataInput in) throws IOException {
        masterAddress = in.readObject();
    }

    @Override
    protected void writeInternal(final ObjectDataOutput out) throws IOException {
        out.writeObject(masterAddress);
    }

    @Override
    protected void toString(StringBuilder sb) {
        super.toString(sb);
        sb.append(", master=").append(masterAddress);
    }

    @Override
    public int getClassId() {
        return ClusterDataSerializerHook.MASTER_RESPONSE;
    }
}
